let my = {
    /**
     * 递归获取所有下级菜单
     *
     * @param current
     * @param menuList
     */
    findChildrenMenus: (current, menuList, layer) => {
        current.title = current.name;
        if (layer <= 1) {
            current.expand = true;
        } else {
            current.expand = false;
        }
        let children = menuList.filter(m => m.parent.id === current.id);
        if (children.length > 0) {
            current.children = children;
            children.forEach(m => my.findChildrenMenus(m, menuList, layer ++));
        }
    },

    /**
     * 获取菜单根结点行为
     */
    getRootMenuOp: () => {
        return {
            render: (h, {root, node, data}) => {
                return h('span', {
                    style: {
                        display: 'inline-block',
                        width: '100%'
                    }
                }, [
                    h('span', [
                        h('Icon', {
                            props: {
                                type: 'ios-menu'
                            },
                            style: {
                                marginRight: '8px'
                            }
                        }),
                        h('span', data.title)
                    ]),
                    h('span', {
                        style: {
                            display: 'inline-block',
                            float: 'right',
                            marginRight: '32px'
                        }
                    }, [
                        h('Button', {
                            props: {
                                size: 'small',
                                icon: 'ios-add',
                                type: 'primary'
                            },
                            style: {
                                width: '64px'
                            },
                            on: {
                                click: () => {
                                    // this.append(data)
                                    console.log("在根结点添加菜单")
                                }
                            }
                        })
                    ])
                ]);
            }
        }
    }
};

let util = {

    /**
     * 获取菜单根结点行为
     */
    getRootMenuOp: (vm) => {
        return {
            render: (h, {root, node, data}) => {
                return h('span', {
                    style: {
                        display: 'inline-block',
                        width: '100%'
                    }
                }, [
                    h('span', [
                        h('Icon', {
                            props: {
                                type: 'ios-menu'
                            },
                            style: {
                                marginRight: '8px'
                            }
                        }),
                        h('span', data.title)
                    ]),
                    h('span', {
                        style: {
                            display: 'inline-block',
                            float: 'right',
                            marginRight: '32px'
                        }
                    }, [
                        h('Button', {
                            props: {
                                size: 'small',
                                icon: 'ios-add',
                                type: 'primary'
                            },
                            style: {
                                width: '64px'
                            },
                            on: {
                                click: () => {
                                    console.log("在根结点添加菜单:" + JSON.stringify(data));
                                    vm.append(data);
                                }
                            }
                        })
                    ])
                ]);
            }
        }
    },

    /**
     * 生成菜单树
     *
     * @param menuList
     */
    formatMenuTree: (menuList) => {
        let rootMenu = menuList.filter(m => !m.parent);
        if (!!rootMenu && rootMenu.length > 0) {
            rootMenu.forEach(root => {
                root.render = my.getRootMenuOp().render;
                my.findChildrenMenus(root, menuList.filter(m => !!m.parent), 0);
            });
            console.log(JSON.stringify(rootMenu));
            return rootMenu;
        }
    }
};

export default util;